function [] = bbr2MM(fname_bbr,fname_MM)
% clear; clc; close all; fname_bbr = 'C:\Users\Mittal\Documents\Work\Code\survival-analysis\perl\LR_CDS_NTDB_TrainCDS+NTDB_TestCDS_Imputed\35-65\CDS_NTDB_TrainRowsCDS+NTDBColsCDS+NTDB_TestRowsCDS+NTDBColsCDS+NTDB_Imputation_HD_CDS_1_NTDB_1_35-65_1_mice_imputed_0.txt';

fprintf('Reading bbr ... ');
fid = fopen(fname_bbr,'r');
tline = fgets(fid);
row = 1;

count = 1;
while ischar(tline),
    if(mod(row,100) == 0)
        fprintf('Processing row %d\n',row);
    end
    components = strsplit(tline,' ');
    parts = cellfun(@(x) cellfun(@str2num,strsplit(x,':')),components,'UniformOutput',false);
    if(~isempty(parts{1} == 1)) % LR-style data
        parts{1} = [0 parts{1}];
    end
    parts = reshape(cell2mat(parts),2,length(parts))';
    if(parts(1,2) == 1)
        data(count,1) = row;
        data(count,2) = 1;
        data(count,3) = 1;
        count = count + 1;
    end
    parts(1,:) = [];
    data(count:size(parts,1)+count-1,1) = row; %row
    data(count:size(parts,1)+count-1,2) = parts(:,1); %column
    data(count:size(parts,1)+count-1,3) = parts(:,2); %value
    count = count + size(parts,1);
    tline = fgets(fid);
    row = row + 1;
end
fclose(fid);

data = sortrows(data,2);
fid = fopen(fname_MM, 'w');
% fprintf(fid,'%d %d %d\n',row,max(data(:,2)),size(data,1));
for i = 1:size(data,1),
    if(mod(i,10000) == 0)
        fprintf('Writing row %d\n',i);
    end

    fprintf(fid,'%d %d %d\n',data(i,1),data(i,2),data(i,3));
end

fclose(fid);

fprintf('Done.');
